import {getRDCourseDetail,getteacherList} from "../../api/classes";
import {useStore} from "vuex";
import {reactive, ref, Ref} from "vue";
import {ReactiveVariable} from "vue/macros";
import {FetchRDCourseDetailDAT} from "../../types/responseTypes";

export function useRDCourseDetail(){
    const {state} = useStore()

    let courseDetail = ref({}) as Ref<FetchRDCourseDetailDAT>
	const teacherList = ref<any>([])
    function initRDCourseDetail(COURSE_ID: string){
        return getRDCourseDetail({
            COURSE_ID,
            OPERATOR_ID: state.user.userInfo.CUSER_ID
        }).then(res => {
            if (res.ERROR[0].errcode == '0'){
                courseDetail.value = res.DAT[0]
            }else {
                uni.showToast({
                    title: res.ERROR[0].error,
                    icon: 'none',
                    mask: true
                })
            }
        }).catch(err => {
            uni.showToast({
                title: '获取信息失败',
                icon: 'error',
                mask: true
            })
        })
    }
	function initgetteacherList(){
		return new Promise((resolve,reject)=>{
			getteacherList({
				TEACHERNAME:"",
				PAGEINDEX:"",
				PAGESIZE:"2000",
				OPERATOR_ID:state.user.userInfo.CUSER_ID
			})
			.then(res=>{
				let List = []    //重组老师列表picker
				for(let i = 0;i<res.DAT.length;i++){
					let obj = {
						value:res.DAT[i].CUSER_ID,
						label:res.DAT[i].TEACHERNAME,
						TEACHERNO:res.DAT[i].TEACHERNO,
						rowid:res.DAT[i].rowid
					}
					List.push(obj)
				}
				teacherList.value = List
			})
		})
	}
    return {
        courseDetail,
        initRDCourseDetail,
		initgetteacherList,
		teacherList
    }
}
